package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description
 * @Author shh
 * @Date 2025-10-12
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 新增购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        log.info("新增购物车：{}", dto);
        //1.创建shoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //2.补充属性值
        BeanUtils.copyProperties(dto, shoppingCart);
        //判断是否为菜品还是套餐
        //判断该商品是否已存在购物车中

        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart= shoppingCartMapper.selectOne(shoppingCart);
        if(cart==null){
            if(shoppingCart.getDishId()!=null){
                //菜品
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else if (shoppingCart.getSetmealId()!=null){
                //套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());

            } else {
                throw new IllegalArgumentException("购物车中必须包含菜品或套餐");
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //3.将购物车对象插入数据库
            shoppingCartMapper.insert(shoppingCart);
        }else {
            //购物车中已存在该商品，数量+1
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }

        //调用mapper


    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        log.info("查询购物车");
        return shoppingCartMapper.selectByUserId(BaseContext.getCurrentId());
    }

        /**
         * 删除购物车
         * @param dto
         */
    @Override
    public void delete(ShoppingCartDTO dto) {
        log.info("清空购物车：{}", dto);
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    /**
     * 减少购物车商品数量
     *
     * @param dto
     */
    //涉及多表需要使用事务
    @Transactional
    @Override
    public void sub(ShoppingCartDTO dto) {
        //注意购物车中的商品包含菜品和套餐，当菜品和套餐被删除时对应的口味也要从口味表中删除掉
        //1.根据用户id和商品id查询购物车中该商品的数量
        ShoppingCart shoppingCart = new ShoppingCart();//
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id
        ShoppingCart cart = shoppingCartMapper.selectOne(shoppingCart);//根据用户id和商品id查询购物车中该商品的数量
        if(cart==null){
            throw new IllegalArgumentException("购物车中不存在该商品");
        }
        //2.判断该商品数量是否为1
        if(cart.getNumber()==1){
            //3.如果是1，则删除该商品
            shoppingCartMapper.delete(cart.getId());
        }else {
            //4.如果不是1，则数量-1
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.update(cart);
        }
    }
}
